﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FDSimpleModelGenerator.Entities;
using FDSimpleModelGenerator.IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TemplateModel = FDSimpleModelGenerator.Models.ClassTemplates.ClassTemplateModel;

namespace FDSimpleModelGenerator.Pages
{
    /// <summary>
    /// 类模板管理
    /// </summary>
    [Authorize]
    public class ClassTemplateModel : PageModel
    {
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IClassTemplateCategoryService _classTemplateCategoryService;
        private readonly IClassTemplateService _classTemplateService;
        public IList<TemplateModel> Templates { get; private set; }
        public IList<NameValue<int>> Categories { get; private set; }
        public IList<NameValue<int>> LanguageDataTypes { get; private set; }

        public ClassTemplateModel(IWebHostEnvironment hostingEnvironment, IClassTemplateService classTemplateService,
            IClassTemplateCategoryService classTemplateCategoryService)
        {
            _classTemplateService = classTemplateService;
            _classTemplateCategoryService = classTemplateCategoryService;
            _hostingEnvironment = hostingEnvironment;

            //TemplatePath = Path.Combine(_hostingEnvironment.WebRootPath, "Templates");
        }

        public void OnGet()
        {
            Categories = _classTemplateCategoryService.GetList().Select(c => new NameValue<int> { Name = c.Name, Value = c.Id }).ToList();
            var dicCategories = new Dictionary<int, string>();
            foreach (var category in Categories)
            {
                dicCategories[category.Value] = category.Name;
            }
            LanguageDataTypes = new List<NameValue<int>>
            {
                new NameValue<int> { Name = "C#", Value = 0 },new NameValue<int> { Name = "Java", Value = 1 },new NameValue<int> { Name = "Jdbc", Value = 2 }
            };

            Templates = _classTemplateService.GetList().Select(c => new TemplateModel
            {
                Id = c.Id,
                Name = c.Name,
                FileName = c.FileName,
                CreateTime = c.CreateTime,
                Category = c.Category,
                CategoryName = dicCategories[c.Category],
                Description = c.Description,
                DataType = c.DataType
            }).ToList();

        }

        public async Task<IActionResult> OnPostAdd([FromBody] TemplateModel model)
        {
            if (!ModelChecker.Check(ModelState, out string errMsg))
            {
                return new JsonResult(new { Code = 400, Message = errMsg });
            }
            var res = await _classTemplateService.AddAsync(new ClassTemplate
            {
                Name = model.Name,
                Description = model.Description,
                Category = model.Category,
                FileName = model.FileName,
                DataType = model.DataType
            });

            if (!res)
            {
                return new JsonResult(new { Code = 500, Message = "Error for add" });
            }

            return new JsonResult(new { Code = 0, Message = "Ok" });
        }

        public async Task<IActionResult> OnPostEdit([FromBody] TemplateModel model)
        {
            if (!ModelChecker.Check(ModelState, out string errMsg))
            {
                return new JsonResult(new { Code = 400, Message = errMsg });
            }

            var template = await _classTemplateService.GetAsync(model.Id);
            if (template == null)
            {
                return new JsonResult(new { Code = 400, Message = "不存在" });
            }

            template.Name = model.Name;
            template.Description = model.Description;
            template.Category = model.Category;
            template.FileName = model.FileName;
            template.DataType = model.DataType;

            var res = await _classTemplateService.UpdateAsync(template);
            if (!res)
            {
                return new JsonResult(new { Code = 500, Message = "Error for update" });
            }
            return new JsonResult(new { Code = 0, Message = "Ok" });
        }
    }
}